--
-- Description: Script to save a cab record.
-- Steps include creating a new record in the cabs table.
-- Date created: 5 May 2008
--

set ANSI_NULLS on
set QUOTED_IDENTIFIER on
go

--
-- drop the stored procedure if it exists and recreate it.
--
if exists
(
	select * 
	from sys.objects
	where object_id = object_id(N'[saveCab_sp]')
	and type in (N'P', N'PC')
)
begin
    drop procedure [dbo].[saveCab_sp]
end
go

create procedure [dbo].[saveCab_sp]
(
	@CabID int
    , @CabSizeInBytes bigint
	, @CabFileNameWithPath varchar(2048)
	, @DateCreated smalldatetime
	, @DateModified smalldatetime
	, @EventID bigint
    , @EventTypeName nvarchar(50)
)
as
begin
	declare @EventTypeID smallint

	--
	-- get the EventTypeID from the event type name.
	--
	exec @EventTypeID = [dbo].[SaveEventType_sp] 
						@EventTypeName = @EventTypeName
	
	--
	-- check if the cab id exists in the Cabs table.
	--
	if not exists
	(
		select top 1 1
		from dbo.Cabs
		where CabID = @CabID
	)
	begin
		--
		-- create a new record in the Cabs table.
		--
		insert into [dbo].[Cabs]
		(
			[CabID]
			, [CabSizeInBytes]
			, [CabFileNameWithPath]
			, [DateCreated]
			, [DateModified]
			, [EventID]
			, [EventTypeID]
		)
		values
		(
			@CabID
			, @CabSizeInBytes
			, @CabFileNameWithPath
			, @DateCreated
			, @DateModified
			, @EventID
			, @EventTypeID
		)
	end
end
go

set ansi_nulls off
go
set quoted_identifier off
go
